package com.example.androidlearning2200.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidlearning2200.R;
import com.example.androidlearning2200.bean.EntryBean;
import com.example.androidlearning2200.bean.MsgBean;

import java.util.List;
import java.util.Random;

public abstract class EntryAdapter extends RecyclerView.Adapter<EntryAdapter.RVHolder> {

    private List<EntryBean> datas;

    public abstract void clickItem(int position);

    public EntryAdapter(List<EntryBean> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_fun_entry, parent, false);
        return new RVHolder(view);
    }

    /**
     * 给每一个列表项填充内容
     *
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull RVHolder holder, int position) {
        EntryBean entryBean = datas.get(position);
        Random random = new Random();
        holder.tvFlag.setBackgroundColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        holder.tvName.setText(entryBean.getFunName());
        holder.tvDesc.setText(entryBean.getFunDesc());
        holder.tvTime.setText(entryBean.getTime());
        holder.itemView.setOnClickListener(view -> clickItem(position));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    /**
     * 列表项包装器
     */
    public class RVHolder extends RecyclerView.ViewHolder {

        private TextView tvFlag;
        private TextView tvName;
        private TextView tvDesc;
        private TextView tvTime;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            tvFlag = itemView.findViewById(R.id.tvFlag);
            tvName = itemView.findViewById(R.id.tvName);
            tvDesc = itemView.findViewById(R.id.tvDesc);
            tvTime = itemView.findViewById(R.id.tvTime);
        }

    }

}
